package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class SettingActivity extends AppCompatActivity {

    private static final String TAG = "Rate";
    private EditText dollarInput,euroInput,wonInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_setting);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        dollarInput = findViewById(R.id.edit_dollar);
        euroInput = findViewById(R.id.edit_euro);
        wonInput = findViewById(R.id.edit_won);

        // 获得数据
        Intent intent = getIntent();
        float dollar = intent.getFloatExtra("dollar_key",0.14f);
        float euro = intent.getFloatExtra("euro_key",0.12f);
        float won = intent.getFloatExtra("won_key",201.81f);

        Log.i(TAG, "onCreate: dollar"+dollar);
        Log.i(TAG, "onCreate: euro"+euro);
        Log.i(TAG, "onCreate: won"+won);

        dollarInput.setText(String.valueOf(dollar));
        euroInput.setText(String.valueOf(euro));
        wonInput.setText(String.valueOf(won));



    }

    public void save(View btn){
        // get new rate
        String dollarStr = dollarInput.getText().toString();
        String euroStr = euroInput.getText().toString();
        String wonStr = wonInput.getText().toString();

        //String-->float
        float dollar = Float.parseFloat(dollarStr);
        float euro =Float.parseFloat(euroStr);
        float won = Float.parseFloat(wonStr);

        Log.i(TAG, "save: dollar="+dollar);
        Log.i(TAG, "save: euro="+euro);
        Log.i(TAG, "save: won="+won);

        Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putFloat("dollar2",dollar);
        bundle.putFloat("euro2",euro);
        bundle.putFloat("won2",won);
        intent.putExtras(bundle);
        setResult(7,intent);

        finish();

    }
}